<?php

namespace addons\recharge\model;

use app\common\model\User;
use think\Db;
use think\Model;

/**
 * 充值订单模型
 */
class Order extends Model
{

    // 表名
    protected $name = 'recharge_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [];

//    public function getStatusAttr($value, $data)
//    {
//        $value = array_filter((array)json_decode($value, true));
//        $value = array_merge(['email' => 0, 'mobile' => 0], $value);
//        return (object)$value;
//    }

    /**
     * 充值结算
     */
    public static function rechargeBalance($id)
    {

        Db::startTrans();

        try{// 提交事务
            $info = self::get($id);

            if($info->status == 'created'){

                $info->save(['status' => 'paid']);
                $money = $info->amount;

                User::money($money,$info->user_id,'充值');//更新用户余额

                $config = get_addon_config('recharge');

                if($config['iscustommoney'] == 1) {
                    User::setTeamMoney($info->user_id,$money,"充值佣金",$config); //设置团队佣金
                }

            }else {
                var_dump('系统问题');
            }

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

}
